* PROGRAM: "PK coding scripts.sps" .
* WRITTEN BY: Matthew DeBell.
* PURPOSE:  This program contains SPSS syntax to machine-code      
  answers to open-ended questions on political knowledge from   
  the ANES 2008 Time Series study. 
* STATUS: complete 20110805.

* NOTE: The program requires an input data file in which the open-ended data are stored in variables called roberts, pelosi, cheney, and brown .
* Data can be prepared from an Excel file here: 
* ftp://ftp.electionstudies.org/ftp/nes/studypages/2008prepost/
* anes2008TSredacted.zip


* ROBERTS.

* SET ALL RESPONSE VARIABLES TO LOWER-CASE.
do repeat v= roberts .
  compute v = lower(v) .
end repeat.
exe.

*** SCHEME 1.
**** CJSC or CJUS .
* Rule: Code correct if the words are 'chief justice' or 'cheif justice' combined with 'supreme court' or 'united states' or 'us'; else code incorrect.
* Note: 'us' is counted as referring to the United States if it is preceded or followed by a space, so that "chief justice of the us" and
"us chief justice" are counted as correct but the 'us' characters in 'justice' do not trigger this credit; note however that words
like "useful" or "doofus" could trigger false positives .
* This kind of false positive is very unlikely because of the requirement that "chief justice" also appear .
compute rob1 = 0.
do repeat v1=roberts.
if ( (index(v1, 'chief justice'))>0 or (index(v1, 'cheif justice'))>0  or (index(v1, 'cj'))>0 ) and 
( (index(v1, 'supreme court'))>0 or (index(v1, 'surpreme court'))>0 or (index(v1, 'united states'))>0 or (index(v1, ' us'))>0 or (index(v1, 'us '))>0 or (index(v1, ' sc'))>0  or (index(v1, 'u.s.'))>0 ) rob1=1.
end repeat.
* code to inapplicable if post-election interview was not completed .
if v081001=0 rob1=-1.

*** SCHEME 2.
**** head of high court .
* Rule: Code correct (1) if the response means head or chief judge or justice in the US 
     or on the high or supreme court; else code incorrect (0). (This is a permissive version of scheme 1.)
* Note: 'us' is counted as referring to the United States if it is preceded or followed by a space, so that "chief justice of the us" and
"us chief justice" are counted as correct but the 'us' characters in 'justice' do not trigger this credit; note however that words
like "useful" or "doofus" could trigger false positives .
* This kind of false positive is very unlikely because of the requirement that "chief" or a similar superlative also appear .
compute rob2 = 0.
do repeat v1=roberts.
if 
  (
      (  
      (index(v1, 'chief'))>0 or (index(v1, 'cheif'))>0 or (index(v1, 'head'))>0 or (index(v1, 'top judge'))>0 
       ) 
   and 
      ( 
      (index(v1, 'supreme court'))>0 or (index(v1, 'high court'))>0 or (index(v1, ' sc'))>0 
      )
  )
or  
  (
      ( 
       (index(v1, 'chief judge'))>0 or (index(v1, 'cheif judge'))>0 or (index(v1, 'head judge'))>0 or (index(v1, 'top judge'))>0 
       or  (index(v1, 'chief justice'))>0 or (index(v1, 'cheif justice'))>0 or (index(v1, 'head justice'))>0 or (index(v1, 'top justice'))>0 
       or  (index(v1, 'chief just'))>0 or (index(v1, 'cheif just'))>0 or (index(v1, 'head just'))>0 or (index(v1, 'top just'))>0 
       ) 
   and  
       (  
       (index(v1, 'supreme court'))>0 or (index(v1, 'high court'))>0 or (index(v1, ' sc'))>0  
       or (index(v1, 'united states'))>0 or (index(v1, ' us'))>0 or (index(v1, 'us '))>0 or (index(v1, 'u.s'))>0
       )
  )
rob2=1.
end repeat.
* code to inapplicable if post-election interview was not completed .
if v081001=0 rob2=-1.

*** SCHEME 3.
*** SAYS CHIEF JUSTICE .
* Rule: Code 1 if the words 'chief' and 'justice' are present, including common misspellings, else code 0.

compute rob3 = 0.
DO REPEAT V1 = ROBERTS .
if ( (index(v1, 'cheif'))>0 or (index(v1, 'chief'))>0  or (index(v1, 'chef'))>0 ) and ( (index(v1, 'justice'))>0 ) rob3=1.
END REPEAT .
* code to inapplicable if post-election interview was not completed .
if v081001=0 rob3=-1.

*** SCHEME 4 .
*** COUNT of keywords: chief, justice, supreme, court, judge. 
* Rule: start from 0 and count the appearance of chief, justice, judge, court, and supreme (including many misspellings) .
compute rob4=0.
DO REPEAT V1=ROBERTS.
if (index(v1, 'cheif'))>0   or (index(v1, 'chief'))>0  or (index(v1, 'chef'))>0 rob4= 1.
if (index(v1, 'justice'))>0  rob4= rob4+1.
if (index(v1, 'court'))>0 or (index(v1, ' crt'))>0 or (index(v1, ' ct'))>0     rob4= rob4+1.
if (index(v1, 'supreme'))>0 or (index(v1, 'supr'))>0 or (index(v1, 'surpreme'))>0  rob4= rob4+1.
if (index(v1, 'judge'))>0   or (index(v1, 'jud'))>0   rob4= rob4+1.
END REPEAT .
* code to inapplicable if post-election interview was not completed .
if v081001=0 rob4=-1.

*** SCHEME 5.
*** score 1 or 2 .
* Rule: Grade the answer 2 points for an answer that indicates Roberts is "chief justice" 
*    or "head of the supreme court" or similar, and half credit (1 point) for "judge" 
*    or "supreme court" or similar.
 
compute rob5=0.
if rob4 > 0 rob5=1.
if (rob1 =1 or rob2=1 or rob3=1) rob5=2 .
* code to inapplicable if post-election interview was not completed .
if v081001=0 rob5=-1.
exe.

* end ROBERTS .



** PELOSI.

* SET ALL RESPONSE VARIABLES TO LOWER-CASE.
do repeat v= pelosi .
  compute v = lower(v) .
end repeat.
exe.

* Pelosi scheme 1. 
* Rule: code correct if the response contains words "house" and "speaker", else incorrect. 
compute pel1 = 0.
do repeat v1=pelosi.
if ( (index(v1, 'house'))>0 ) and 
( (index(v1, 'speaker'))>0 or (index(v1, 'spkr'))>0 ) pel1=1.
end repeat.
* code to inapplicable if post-election interview was not completed .
if v081001=0 pel1=-1.


* Pelosi scheme 2. 
* Rule: grade the answer 2 if scheme1=1, else if the response contains the words congress or rep or representative or house (with credit for
* misspellings) grade the answer 1, else 0. 

compute pel2 = 0.
do repeat v1=pelosi.
if (index(v1, 'house'))>0 or (index(v1, 'rep '))>0 or (index(v1, 'representative'))>0  pel2=1.
end repeat.
if pel1=1 pel2=2. 
* code to inapplicable if post-election interview was not completed .
if v081001=0 pel2=-1.

* Pelosi scheme 3.
* Rule: Code correct if response means head/leder/speaker of the House or Congress, else incorrect. 
* Specifically naming an incorrect office such as "Majority leader" is wrong; general descriptions, if approximately accurate 
* such as "head of house" or "head of congress" are ok.
compute pel3 = 0.
do repeat v1=pelosi.
if (
    (index(v1, 'house'))>0 or (index(v1, 'congres'))>0 
    ) 
  and 
    (
    (index(v1, 'speaker'))>0 or (index(v1, 'chief'))>0  or (index(v1, 'head'))>0  or (index(v1, 'leader'))>0  or (index(v1, 'speak'))>0 
    )
  and
     (index(v1, 'majority leader'))=0
pel3=1 .
end repeat.
* code to inapplicable if post-election interview was not completed .
if v081001=0 pel3=-1.



* BROWN .

* SET ALL RESPONSE VARIABLES TO LOWER-CASE.
* Substitute actual string variable names for v1, etc .
do repeat v= brown .
  compute v = lower(v) .
end repeat.
exe.

* Brown scheme 1. 
* Rule: code correct if the response is "PM" or "Prime Minister" of "UK" or "United Kingdom" or "Britain" or "British" or "England" or "English" . 
compute bro1 = 0.
do repeat v1=brown.
if 
( 
    (  (index(v1, 'pm'))>0 or (index(v1, 'prime min'))>0  )
  and
     (   
       (index(v1, 'uk'))>0 or (index(v1, 'u.k'))>0 or (index(v1, 'united king'))>0  or (index(v1, 'brit'))>0 or (index(v1, 'engl'))>0  
      )
 ) 
bro1=1.
end repeat.
* code to inapplicable if post-election interview was not completed .
if v081001=0 bro1=-1.


* Brown scheme 2. 
* This is a more relaxed version of scheme 1. 
* Rule: code correct if the response means main British leader: "PM" or "Prime Minister" or "head" or "leader" or "president" 
* of "UK" or "United Kingdom" or "Britain" or "British" or "England" or "English" . 
compute bro2 = 0.
do repeat v1=brown.
if 
( 
    (  (index(v1, 'pm'))>0 or (index(v1, 'prime min'))>0 or (index(v1, 'head'))>0 or (index(v1, 'leader'))>0 or (index(v1, 'president'))>0  )
  and
     (   
       (index(v1, 'uk'))>0 or (index(v1, 'u.k'))>0 or (index(v1, 'united king'))>0  or (index(v1, 'brit'))>0 or (index(v1, 'engl'))>0  
      )
 ) 
bro2=1.
end repeat.
* code to inapplicable if post-election interview was not completed .
if v081001=0 bro2=-1.


* Brown scheme 3. 
* This is a more relaxed version of scheme 2.
* Rule: code correct if response indciates Brown is A) a "Prime Minister" or foreign head of government (with our without specifying the country) or B) from the UK, Britain, Engalnd, or London or Scotland. 

compute bro3 = 0.
do repeat v1=brown.
if 
( 
    (  (index(v1, 'pm'))>0 or (index(v1, 'prime min'))>0 or (index(v1, 'foreign leader'))>0 or (index(v1, 'leader of another country'))>0  )
  or
    (   
       (index(v1, ' uk'))>0 or (index(v1, 'uk '))>0 or (index(v1, 'u.k'))>0 or (index(v1, 'united king'))>0 or (index(v1, 'brit'))>0 or (index(v1, 'london'))>0 or (index(v1, 'engl'))>0  
      )
 ) 
bro3=1.
end repeat.
if bro2=1 bro3=1.
* code to inapplicable if post-election interview was not completed .
if v081001=0 bro3=-1.




** CHENEY .

* SET ALL RESPONSE VARIABLES TO LOWER-CASE.
* Substitute actual string variable names for v1, etc .
do repeat v= cheney .
  compute v = lower(v) .
end repeat.
exe.

* Cheney scheme 1. 
* Rule: code correct if the response is VP, VICE PRESIDENT, or VICE PRES, else incorrect. 
compute che1 = 0.
do repeat v1=cheney.
if 
  ( (index(v1, 'vp'))>0 or (index(v1, 'v pres'))>0  or (index(v1, 'v. pres'))>0  or (index(v1, 'v.pres'))>0   )
  or 
   (   (index(v1, 'vice'))>0 and (index(v1, 'pres'))>0   )
che1=1.
end repeat.
* code to inapplicable if post-election interview was not completed .
if v081001=0 che1=-1.
exe .


** IDENTIFY MODAL CODER DECISION.
* In case of ties, use the lead coder's decision, which is reflected in the consensus decision in all cases.

* find the modal answer for Brown1 (0,1).
compute n1 =0.
if brownD1=1 n1=1.
if brownF1=1 n1=n1+1.
if browni1=1 n1=n1+1.
if brownw1=1 n1=n1+1.

compute b1mode=-1.
if n1 ge 3 b1mode=1.
if n1 le 1 b1mode=0.
if n1=2 b1mode=b1.

* find the modal answer for Brown2 (0,1).
compute n1 =0.
if brownD2=1 n1=1.
if brownF2=1 n1=n1+1.
if browni2=1 n1=n1+1.
if brownW2=1 n1=n1+1.

compute b2mode=-1.
if n1 ge 3 b2mode=1.
if n1 le 1 b2mode=0.
if n1=2 b2mode=b2.

cross b1 by b1mode.
cross b2 by b2mode. 

* find the modal answer for Brown3 (0,1).
compute n1 =0.
if brownD3=1 n1=1.
if brownF3=1 n1=n1+1.
if browni3=1 n1=n1+1.
if brownW3=1 n1=n1+1.

compute b3mode=-1.
if n1 ge 3 b3mode=1.
if n1 le 1 b3mode=0.
if n1=2 b3mode=b3.

* find modal answer for Cheney (0,1).
compute n1 =0.
if cheneyD1=1 n1=1.
if cheneyF1=1 n1=n1+1.
if cheneyi1=1 n1=n1+1.
if cheneyW1=1 n1=n1+1.

compute c1mode=-1.
if n1 ge 3 c1mode=1.
if n1 le 1 c1mode=0.
if n1=2 c1mode=c1.

* find the modal answer for Pelosi1 (0,1).
compute n1 =0.
if pelosiD1=1 n1=1.
if pelosiF1=1 n1=n1+1.
if pelosii1=1 n1=n1+1.
if pelosiw1=1 n1=n1+1.

compute p1mode=-1.
if n1 ge 3 p1mode=1.
if n1 le 1 p1mode=0.
if n1=2 p1mode=p1.

* find the modal answer for Pelosi2 (0,1,2).
compute n0 =0.
compute n1 =0.
compute n2=0 .
if pelosiD2=1 n1=1.
if pelosiF2=1 n1=n1+1.
if pelosii2=1 n1=n1+1.
if pelosiw2=1 n1=n1+1.
if pelosiD2=2 n2=1.
if pelosiF2=2 n2=n2+1.
if pelosii2=2 n2=n2+1.
if pelosiW2=2 n2=n2+1.
compute n0 = 4-n1-n2.
if n1>n0 and n1>n2 p2mode=1.
if n2>n0 and n2>n1 p2mode=2.
if n0>n1 and n0>n2 p2mode=0. 

* find the modal answer for Pelosi3 (0,1).
compute n1 =0.
if pelosiD3=1 n1=1.
if pelosiF3=1 n1=n1+1.
if pelosii3=1 n1=n1+1.
if pelosiw3=1 n1=n1+1.

compute p3mode=-1.
if n1 ge 3 p3mode=1.
if n1 le 1 p3mode=0.
if n1=2 p3mode=p3.

* find the modal answer for Roberts1 (0,1).
compute n1 =0.
if robertsD1=1 n1=1.
if robertsF1=1 n1=n1+1.
if robertsi1=1 n1=n1+1.
if robertsw1=1 n1=n1+1.

compute r1mode=-1.
if n1 ge 3 r1mode=1.
if n1 le 1 r1mode=0.
if n1=2 r1mode=r1.

* find the modal answer for Roberts2 (0,1).
compute n1 =0.
if robertsD2=1 n1=1.
if robertsF2=1 n1=n1+1.
if robertsi2=1 n1=n1+1.
if robertsw2=1 n1=n1+1.

compute r2mode=-1.
if n1 ge 3 r2mode=1.
if n1 le 1 r2mode=0.
if n1=2 r2mode=r2.

* find the modal answer for Roberts3 (0,1).
compute n1 =0.
if robertsD3=1 n1=1.
if robertsF3=1 n1=n1+1.
if robertsi3=1 n1=n1+1.
if robertsw3=1 n1=n1+1.

compute r3mode=-1.
if n1 ge 3 r3mode=1.
if n1 le 1 r3mode=0.
if n1=2 r3mode=r3.

* find modal answer for Roberts4 (0,1,2,3,4,5).
compute n0 =0.
compute n1 =0.
compute n2=0 .
compute n3=0.
compute n4=0.
compute n5=0.
compute r4mode = r4.
if robertsD4 = 1 n1 = 1.
if robertsF4 = 1 n1 = n1+1.
if robertsi4  = 1 n1 = n1+1.
if robertsW4=1 n1 = n1+1.

if robertsD4 = 2 n2 = 1.
if robertsF4 = 2 n2 = n2+1.
if robertsi4  = 2 n2 = n2+1.
if robertsW4=2 n2 = n2+1.

if robertsD4 = 3 n3 = 1.
if robertsF4 = 3 n3 = n3+1.
if robertsi4  = 3 n3 = n3+1.
if robertsW4= 3 n3 = n3+1.

if robertsD4 = 4 n4 = 1.
if robertsF4 = 4 n4 = n4+1.
if robertsi4  = 4 n4 = n4+1.
if robertsW4=4 n4 = n4+1.

if robertsD4 = 5 n5 = 1.
if robertsF4 = 5 n5 = n5+1.
if robertsi4  = 5 n5 = n5+1.
if robertsW4=5 n5 = n5+1.

compute n0 = 4-n1-n2-n3-n4-n5 .
if n0>n1 and n0>n2 and n0>n3 and n0>n4 and n0>n5 r4mode = 0.
if n1>n0 and n1>n2 and n1>n3 and n1>n4 and n1>n5 r4mode = 1.
if n2>n0 and n2>n1 and n2>n3 and n2>n4 and n2>n5 r4mode = 2.
if n3>n0 and n3>n1 and n3>n2 and n3>n4 and n3>n5 r4mode = 3.
if n4>n0 and n4>n1 and n4>n2 and n4>n3 and n4>n5 r4mode = 4.
if n5>n0 and n5>n1 and n5>n2 and n5>n3 and n5>n4 r4mode=5. 


* find the modal answer for Roberts5 (0,1,2).
compute n0 =0.
compute n1 =0.
compute n2=0 .
if robertsD5=1 n1=1.
if robertsF5=1 n1=n1+1.
if robertsi5=1 n1=n1+1.
if robertsw5=1 n1=n1+1.
if robertsD5=2 n2=1.
if robertsF5=2 n2=n2+1.
if robertsi5=2 n2=n2+1.
if robertsW5=2 n2=n2+1.
compute n0 = 4-n1-n2.
if n1>n0 and n1>n2 r5mode=1.
if n2>n0 and n2>n1 r5mode=2.
if n0>n1 and n0>n2 r5mode=0. 

* Find number of discrepant cases .
cross b1 by b1mode.
cross b2 by b2mode. 
cross b3 by b3mode. 
cross c1 by c1mode.
cross p1 by p1mode. 
cross p2 by p2mode.
cross p3 by p3mode.
cross r1 by r1mode.
cross r2 by r2mode.
cross r3 by r3mode.
cross r4 by r4mode.
cross r5 by r5mode. 


